package com.opengl.textprint.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;

import com.opengl.textprint.R;

/**
 * Author: Wu Chang
 * <p>
 * Date: 2023/4/24 14:43
 */
public class GameImageGetter implements Html.ImageGetter {

    private int imageAutoWidth;
    private Context context;
    public GameImageGetter(Context context, int width) {
        imageAutoWidth = width;
        this.context = context;
    }

    @Override
    public Drawable getDrawable(final String source) {
        try {
            Bitmap resource = ImageLoad.getBitmapByUrl(context, source);
            Log.d("GameImageGetter", "resource width:" + resource.getWidth() + "; height:"+resource.getHeight());
            int[] size = new int[2];
            Log.d("GameImageGetter", "show width:" + size[0] + "; height:"+size[1]);
            Log.d("GameImageGetter", "screen width:" + imageAutoWidth);
            Drawable drawable = null;
            drawable = new BitmapDrawable(resource);
            int showHeight = imageAutoWidth * resource.getHeight() / resource.getWidth();
            drawable.setBounds(0, 0, imageAutoWidth, showHeight);
            return drawable;
        } catch (Exception e) {
            @SuppressLint("UseCompatLoadingForDrawables")
            Drawable drawable = context.getDrawable(R.mipmap.ic_launcher);
            drawable.setBounds(0, 0, 20, 20);
            Log.d("GameImageGetter", "Exception:" + e.getMessage());
            return drawable;
        }
    }

}